package com.wx.mythread.stopthread;

/**
 * 在每次循环都sleep的时候，我们while条件中不加判断isInterrupted也是可以的
 * 因为执行sleep的时候，线程出发了interrupted会抛出异常
 * java.lang.InterruptedException: sleep interrupted
 */
public class RightWayStopThreadWithEveryLoop {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = ()->{
            try {
                int num = 0;
                // while (num <= 10000 && !Thread.currentThread().isInterrupted()){
                while (num <= 10000 ){
                    if (num % 100 == 0){
                        System.out.println(num+"是100的倍数");
                    }
                    num++;
                    Thread.sleep(10);
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
